/*******************************************************************************
 * FilePath    : /main.c
 * Version     : 1.0
 * Brief       :
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2021-03-03 17:08:51
 * LastEditors : AndrewHu
 * LastEditTime: 2022-02-16 17:27:07
 * Description :
 ******************************************************************************/
/*******************************************************************************
 *    Copyright (c) 2022 Macrogiga Electronics Co., Ltd.
 *
 *    Permission is hereby granted, free of charge, to any person
 *    obtaining a copy of this software and associated documentation
 *    files (the 'Software'), to deal in the Software without
 *    restriction, including without limitation the rights to use, copy,
 *    modify, merge, publish, distribute, sublicense, and/or sell copies
 *    of the Software, and to permit persons to whom the Software is
 *    furnished to do so, subject to the following conditions:
 *
 *    The above copyright notice and this permission notice shall be
 *    included in all copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *    DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function ----------------------------------------------------------*/
/* extern --------------------------------------------------------------------*/

/*******************************************************************************
 * Function    : Key_Run
 * Brief       : 按键信息反馈处理
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-04 14:53:26
 * Description :
 ******************************************************************************/
void Key_Run(void)
{
    u8 event = Key_Scan();
    if ((event & KeyFlag_event) == 0x00)
        return;
    Key_ClearFlag();
    switch (event)
    {
    case (KeyFlag_event | KeyFlag_times | KeyFlag_down | 0x01): //按键按下1次
        // Printf_Error(1);
        break;
    case (KeyFlag_event | KeyFlag_times | 0x01): //按键按下1次后"立即"松开
        // Printf_Error(2);
        break;
    case (KeyFlag_event | KeyFlag_times | KeyFlag_down | 0x03): //按键连按3次
        // Printf_Error(3);
        break;
    case (KeyFlag_event | KeyFlag_times | 0x03): //按键连按3次后"立即"松开
        // Printf_Error(4);
        break;
    case (KeyFlag_event | KeyFlag_down | KeyFlag_Long): //按键长按
        // Printf_Error(5);
        break;
    case (KeyFlag_event | KeyFlag_times | KeyFlag_Long): //按键长按后松开
        // Printf_Error(6);
        break;
    case (KeyFlag_event | KeyFlag_times): //按键松开，非连按状态下的松开
        // Printf_Error(7);
        break;
    default:
        // Printf_Error(event);
        break;
    }
}
/*******************************************************************************
 * Function    : main
 * Brief       : 主函数
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 17:14:47
 * Description :
 ******************************************************************************/
int main(void)
{
    u32 UnitDelay = 0x2000;

    while (UnitDelay--)
        ; // wait a while for hex programming if using the MCU stop mode, default NOT used.

    BSP_Init(); //硬件初始化
    Ble_Init(); //蓝牙初始化
    Uart_Send_String((u8 *)"\r\nIND:Ble Init OK!", 18);
    ble_run_interrupt_start(24);   //广播间隔设定，24*0.625=15 ms
    UnitDelay = GetSysTickCount(); //获取滴答时钟时间，1ms累计
    while (1)
    {
        IrqMcuGotoSleepAndWakeup();
        if ((GetSysTickCount() - UnitDelay) >= 10)
        {
            // 10ms计时
            UnitDelay = GetSysTickCount();

            //按键扫描
            Key_Run();
            Fun_Uart_Run(); //串口扫描
        }
    }
}
